//新增应收订单

/**
 * 总视图
 */
interface IReceivableAddViewModel extends ng.IScope{
    /**
     * 时间日期格式
     */
    dateFormat: "{format:'YYYY.MM.DD HH:mm'}";
    /**
     * 货物类别下拉
     */
    goodsTypeDropDown: GoodsTypeListItemResponse[];
    /**
     * 货物名称下拉
     */
    goodsDropDown: GoodsListItemResponse[];
    /**
     * 货物数量单位下拉
     */
    goodsNumUnitDropDown: ValueListItemResponse[];
    /**
     * 车长下拉
     */
    //carLengthDropDown:ValueListItemResponse[];
    /**
     * 车型下拉
     */
    //carTypeDropDown:ValueListItemResponse[];
    /**
     * 应收(应付)单价单位下拉
     */
    priceUnitDropDown:ValueListItemResponse[];
    /**
     * 独立专员列表
     */
    independentList:EmployeeListItemResponse[];
    /**
     * 计划专员
     */
    planOfficer: EmployeeListItemResponse;
    /**
     * 计划专员id
     */
    planOfficerId: string;
    /**
     * 商务专员
     */
    businessOfficer: EmployeeListItemResponse;
    /**
     * 商务专员id
     */
    businessOfficerId:string;
    /**
     * 客服专员
     */
    customerServiceOfficer: EmployeeListItemResponse;
    /**
     * 客服专员id
     */
    customerServiceOfficerId: string;
    /**
     * 调度专员
     */
    dispatchOfficer: EmployeeListItemResponse;
    /**
     * 调度专员id
     */
    dispatchOfficerId: string;
    /**
     * 客户编号
     */
    clientId: string;
    /**
     * 客户单位名称
     */
    clientName:string;
    /**
     * 发货计划人编号
     */
    consignorId: string;
    /**
     * 发货计划人名称
     */
    consignorName:string;
    /**
     * 发货计划人号码
     */
    consignorPhone:string;
    /**
     * 发货地省编号
     */
    shipProvinceCode: string;
    /**
     * 发货地市编号
     */
    shipCityCode: string;
    /**
     * 发货地区编号
     */
    shipAreaCode: string;
    /**
     * 发货地详细地址
     */
    shipAddress: string;
    /**
     * 送货地省编号
     */
    deliverProvinceCode: string;
    /**
     * 送货地市编号
     */
    deliverCityCode: string;
    /**
     * 发货地区编号
     */
    deliverAreaCode: string;
    /**
     * 发货地详细地址
     */
    deliverAddress: string;
    /**
     * 发货时间
     */
    shipTime: string;
    /**
     * 到货时间
     */
    arriveTime: string;
    /**
     * 货物类别编号
     */
    goodsTypeId: string;
    /**
     * 货物类别
     */
    goodsTypeName: string;
    /**
     * 货物编号
     */
    goodsId: string;
    /**
     * 货物名称
     */
    goodsName: string;
    /**
     * 货物数量
     */
    goodsNum: number;
    /**
     * 货物数量单位
     */
    goodsNumUnit: string;
    /**
     * 应收单价
     */
    receivablePrice: number;
    /**
     * 应收单价单位
     */
    receivablePriceUnit: string;
    /**
     * 应收总价
     */
    receivableTotal: number;
    /**
     * 应收说明
     */
    receivableRemarks: string;
    /**
     * 承运商id
     */
    carrierId:string;
    /**
     * 承运商名称
     */
    carrierName:string;
    /**
     * 结算方号码（承运商号码）
     */
    carrierPhone:string;
    /**
     * 承运商类型
     */
    carrierType:string;
    /**
     * 车辆id
     */
    carId:string;
    /**
     * 车牌号
     */
    carCode:string;
    /**
     * 驾驶员号码
     */
    driverPhone:string;
    /**
     * 驾驶员姓名
     */
    driverName:string;
    /**
     * 车长（承运）
     */
    carLength: string;
    /**
     * 车型（承运）
     */
    carType: string;
    /**
     * 银行卡号
     */
    bankCode:string;
    /**
     * 开户银行
     */
    bankName:string;
    /**
     * 持卡人姓名
     */
    holder:string;
    /**
     * 应付单价
     */
    payablePrice:number;
    /**
     * 应付单价单位
     */
    payablePriceUnit:string;
    /**
     * 驾驶证号
     */
    driverLicenseNumber:string;
    /**
     * 合同号
     */
    contractNumber:string;
    /**
     * 客户单位自动补全
     */
    searchClient:(value:string) => any;
    /**
     * 选择客户单位事件
     */
    clientSelect:(selected:angucomplete<ClientListItemResponse>) => void;
    /**
     * 选择货物类型事件
     */
    changeGoodsType:() => void;
    /**
     * 承运商自动补全
     */
    searchCarrier:(value:string) => any;
    /**
     * 选择承运商事件
     */
    carrierSelect:(selected:angucomplete<CarrierListItemResponse>) => void;
    /**
     * 车牌号自动补全
     */
    searchCar:(value:string) => any
    /**
     * 选择车牌号事件
     */
    carCodeSelect:(selected:angucomplete<CarListItemResponse>) => void;
    /**
     * 保存应收订单事件
     */
    addReceivable:() => void;
    /**
     * 取消
     */
    back:() => void;
}

class ReceivableAddController{
    /**
     * 应收总单编号
     */
    billId:string;
    /**
     * 客户单位短号
     */
    clientCornet:string;

    constructor(private $scope:IReceivableAddViewModel,private carService:ICarService,private bankService:IBankService,private carrierService:ICarrierService,private customerRepresentativeService:ICustomerRepresentativeService,private employeeService:IEmployeeService,private valueService:IValueService,private goodsService:IGoodsService,private goodsTypeService:IGoodsTypeService,private receivableService:IReceivableService,private clientService:IClientService,private $location:ng.ILocationService,private $loading:any,
    private $ngBootbox: BootboxService){
        this.init();
    }

    init():void{
        this.billId = this.$location.search().id;

        this.$scope.goodsTypeDropDown = [];
        this.$scope.goodsDropDown = [];
        this.$scope.independentList = [];
        this.loadDropDown();
        /**
         * 自动补全
         */
        this.$scope.searchClient = this.searchClient;
        this.$scope.searchCarrier = this.searchCarrier;
        this.$scope.searchCar = this.searchCar;

        /**
         * 选择事件
         */
        this.$scope.clientSelect = this.clientSelect;
        this.$scope.changeGoodsType = this.changeGoodsType;
        this.$scope.carrierSelect = this.carrierSelect;
        this.$scope.carCodeSelect = this.carCodeSelect;

        this.$scope.addReceivable = this.addReceivable;
        this.$scope.back = this.back;

        $('#opendeliveryTime').click(function () {
            $('#deliveryTime').datetimepicker('show');
        });
        $('#openarrivalTime').click(function () {
            $('#arrivalTime').datetimepicker('show');
        });
    }

    /**
     * 下拉加载
     */
    loadDropDown():void{
        this.$scope.goodsNumUnitDropDown = this.valueService.getGoodQuantityUnitList().data;
        this.$scope.priceUnitDropDown = this.valueService.getPriceUnitList().data;
        //this.$scope.carLengthDropDown = this.valueService.getCarLengthList().data;
        //this.$scope.carTypeDropDown = this.valueService.getCarTypeList().data;
    }

    /**
     * 客户单位自动补全
     */
    searchClient = (value: string) => {
        return this.clientService.getList(value,"","","","","",0,5).then((res) => {
            return res.data.data;
        });
    }

    /**
     * 选择客户单位事件
     */
    clientSelect = (selected:angucomplete<ClientListItemResponse>) =>{
        if(selected){
            this.$scope.clientId = selected.description.id;
            this.$scope.goodsTypeId = "";
            this.$scope.goodsId = "";       
            this.isIndependentList();
            this.searchConsignor();
            this.loadGoodsTypeDropDown();
        }else{
            return;
        }
    }

    /**
     * 判断客户单位是否含有独立专员
     */
    isIndependentList = () => {
        this.employeeService.getbytype("7",0,-1).then((res)=>{
            if(res.data.data.length < 1){
                this.$ngBootbox.alert("该客户单位下没有独立专员！请添加后再次添加应收订单");
                return;
            }
            this.$scope.independentList = res.data.data;
            this.$scope.businessOfficerId = res.data.data[0].id;
            this.$scope.customerServiceOfficerId = res.data.data[0].id;
            this.$scope.dispatchOfficerId = res.data.data[0].id;
            this.$scope.planOfficerId = res.data.data[0].id;
        });
    }

    /**
     * 获取发货计划人信息
     */
    searchConsignor = () => {
        this.customerRepresentativeService.getList(this.$scope.clientId,"","",0,-1).then((res) =>{
            if(res.data.data.length < 1){
                this.$ngBootbox.alert("该客户单位下没有发货计划人！请添加后再次添加应收");
                return;
            }
            this.$scope.consignorName = res.data.data[0].realName;
            this.$scope.consignorPhone = res.data.data[0].phoneNumber;
        });
    }

    /**
     * 获取货物类别下拉
     */
    loadGoodsTypeDropDown = ()=>{
        this.goodsTypeService.getList(this.$scope.clientId,"",0,-1).then((res) => {
            this.$scope.goodsTypeDropDown = res.data.data;
            if(this.$scope.clientId == undefined || this.$scope.clientId == ""){
                this.$scope.goodsTypeDropDown = [];
                this.$scope.goodsTypeId = "";
            }
        });
    }

    /**
     * 选择货物类型事件
     */
    changeGoodsType = () => {
        this.loadGoodsDropDown(this.$scope.goodsTypeId);
    }
    /**
     * 获取货物名称下拉
     */
    loadGoodsDropDown = (goodsTypeId:string) => {
        this.goodsService.getList(this.$scope.clientId, "",goodsTypeId, 0, -1).then(result => {
            this.$scope.goodsDropDown = result.data.data;
            if (this.$scope.goodsTypeId == undefined || this.$scope.goodsTypeId == ""){
                this.$scope.goodsDropDown = [];
                this.$scope.goodsId = "";
            };
        });
    }

    /**
     * 承运商自动补全
     */
    searchCarrier = (value:string) =>{
        return this.carrierService.getList("",true, value, "", "", "", "", "",true, 0, 5).then((res) => {
            return res.data.data;
        });
    }

    /**
     * 选择承运商事件
     */
    carrierSelect = (selected:angucomplete<CarrierListItemResponse>) =>{
        if(selected){
            this.$scope.carrierId = selected.description.id;
            this.$scope.carrierPhone = selected.description.phone;
            this.$scope.carrierType = selected.description.type;
            this.searchCarrierBank(this.$scope.carrierId);
        }
    }

    /**
     * 获取承运商银行卡信息
     */
    searchCarrierBank = (carrierId:string) =>{
         this.bankService.getList("", carrierId, "", false, 0, -1).then((res) => {
            if (res.data.data.length < 1) {
                this.$scope.bankCode = "";
                this.$scope.bankName = "";
                this.$scope.holder = "";
                this.$ngBootbox.alert("该承运商下没有银行卡号！请添加后再次添加应收"); 
                return;
            }
            this.$scope.bankCode = res.data.data[0].cardNumber;
            this.$scope.bankName = res.data.data[0].bank;
            this.$scope.holder = res.data.data[0].holder;
        });
    }

    /**
     * 车牌号自动补全
     */
    searchCar = (value:string) => {
        return this.carService.getList(null, true , value, "", "", "", "", "", "", "",true,"","",0, 5).then((res) => {
            return res.data.data;
        });
    }

    /**
     * 选择车牌号事件
     */
    carCodeSelect = (selected:angucomplete<CarListItemResponse>) => {
        if (selected) {
            this.$scope.carId = selected.description.id;
            this.$scope.carCode = selected.description.carCode; 
            this.$scope.driverPhone = selected.description.driverPhone;
            this.$scope.driverName = selected.description.driver;
            this.$scope.carLength= selected.description.carLength;
            this.$scope.carType = selected.description.carType;
            this.searchCarBank(this.$scope.carId);
        } else { return };
    }

    /**
     * 获取车辆银行卡信息
     */
    searchCarBank = (carId:string) =>{
        if(this.$scope.carrierId == undefined || this.$scope.carrierId == ""){
            this.bankService.getList(carId, "", "", false, 0, -1).then((res) => {
                if (res.data.data.length < 1) {
                    this.$scope.bankCode = "";
                    this.$scope.bankName = "";
                    this.$scope.holder = "";
                    return;
                }
                this.$scope.bankCode = res.data.data[0].cardNumber;
                this.$scope.bankName = res.data.data[0].bank;
                this.$scope.holder = res.data.data[0].holder;
            });
        }
    }

    /**
     * 保存应收订单事件
     */
    addReceivable = () =>{
        if (!$("#reconform").valid()) { return };
        this.$ngBootbox.confirm('确定发布此单吗?').then(() => {
            this.$loading.start('receivable');
            //获取客户短号
            this.clientService.getDetail(this.$scope.clientId).then((res) => {
                this.clientCornet = res.data.cornet;
                    this.receivableService.addnewreceivable(
                        this.$scope.clientId,
                        this.$scope.shipProvinceCode,
                        this.$scope.shipCityCode,
                        this.$scope.shipAreaCode,
                        this.$scope.shipAddress,
                        this.$scope.deliverProvinceCode,
                        this.$scope.deliverCityCode,
                        this.$scope.deliverAreaCode,
                        this.$scope.deliverAddress,
                        this.$scope.goodsId,
                        this.$scope.goodsTypeId,
                        this.$scope.goodsNum,
                        this.$scope.goodsNumUnit,
                        this.$scope.payablePrice,
                        this.$scope.payablePriceUnit,
                        this.$scope.receivablePrice,
                        this.$scope.receivablePriceUnit,
                        this.$scope.receivableTotal,
                        this.$scope.receivableRemarks,
                        this.$scope.carrierId,
                        this.$scope.carId,
                        this.$scope.contractNumber,
                        this.$scope.driverLicenseNumber,
                        this.$scope.bankName,
                        this.$scope.bankCode,
                        this.$scope.holder,
                        this.$scope.shipTime,
                        this.$scope.arriveTime,
                        this.billId,
                        this.clientCornet
                ).then((res) => {
                    this.$loading.finish('receivable');
                    window.history.back();
                },(rej) => {
                    this.$loading.finish('receivable');
                    
                });
            })

        }, () => { return; });
    }

    /**
     * 取消事件
     */
    back():void{
        window.history.back();
    }
}

angular.module("tms").controller("receivableAddController",["$scope","carService","bankService","carrierService","customerRepresentativeService","employeeService","valueService","goodsService","goodsTypeService","receivableService","clientService","$location","$loading","$ngBootbox",ReceivableAddController]); 